/**
 * 
 */
package com.hst.web.sys.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.hst.core.Page;
import com.hst.core.ServiceContext;
import com.hst.core.ServiceException;
import com.hst.core.annotation.HttpCache;
import com.hst.core.auth.IUser;
import com.hst.core.auth.IUserAuth;
import com.hst.core.json.JsonUtil;
import com.hst.core.meta.IMetaUpdate;
import com.hst.core.meta.MetaAuth;
import com.hst.core.meta.MetaInfo;
import com.hst.core.meta.MetaRepository;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/**
 * @author duyong
 *
 */
@Controller
@RequestMapping("/metaapp/{name}")
public class MetaAppController {
	private MetaRepository mr;
	//private IUserAuth userAuth;

	public MetaAppController(MetaRepository mr, IUserAuth userAuth) {
		this.mr = mr;
		//this.userAuth = userAuth;
	}

//	protected void checkAuth(String name, boolean isEdit) throws ServiceException {
//		MetaAuth auth = mr.getMetaAuth(name);
//		if (null == auth)
//			return;
//
//		IUser u = (IUser) ServiceContext.getInstance().getUser();
//		if (null == u)
//			throw new ServiceException(IUserAuth.AUTH_NOUSER);
//
//		if (!userAuth.hasAuth(u, auth.getAuth()))
//			throw new ServiceException(IUserAuth.AUTH_NOAUTH);
//
//		if (isEdit && !userAuth.hasAuth(u, auth.getEdit()))
//			throw new ServiceException(IUserAuth.AUTH_NOAUTH);
//	}

	@RequestMapping
	@HttpCache()
	public Model idx(@PathVariable String name, Model m) throws ServiceException {
		//checkAuth(name, false);
		MetaInfo mi = mr.getMetaInfo(name);
		if (null == mi)
			throw new ServiceException("1");
		m.addAttribute("meta", mi);
		return m;
	}

	protected Map<String, ?> parseQuery(String name, String data)
			throws JsonParseException, JsonMappingException, IOException {
		return JsonUtil.read(data, Map.class);
	}

	@RequestMapping(value = "/query", method = { RequestMethod.POST })
	public void query(@PathVariable String name, @RequestBody String data, Model m) throws ServiceException {
		//checkAuth(name, false);
		try {
			// condition
			Map<String, ?> args = parseQuery(name, data);

			// query
			List<?> datas = mr.getMetaProxy(name).list(args);

			m.addAttribute(name, datas);
		} catch (IOException e) {
			throw new ServiceException("1", e.getMessage(), e);
		}
	}

	@RequestMapping(value = "/export", method = { RequestMethod.POST })
	public void export(@PathVariable String name,String data, Model m) throws ServiceException, UnsupportedEncodingException {
		query(name, data, m);
	}

	@RequestMapping(value = "/{page}", method = { RequestMethod.POST })
	public void queryPage(@PathVariable String name, @PathVariable int page, String pageSize, @RequestBody String data, Model m)
			throws ServiceException {
		int _pageSize = 0;
		if (null != pageSize && !"".equals(pageSize))
			_pageSize = Integer.parseInt(pageSize);
		
		Page.setPageSize(_pageSize);
		
		//checkAuth(name, false);
		try {
			// condition
			Map<String, ?> args = parseQuery(name, data);

			// query
			Page<?> datas = mr.getMetaProxy(name).list(args, page);

			m.addAttribute(name, datas);
		} catch (IOException e) {
			throw new ServiceException("1", e.getMessage(), e);
		}
	}

	/**
	 * 更新数据
	 * 
	 * @param name
	 * @param data
	 * @param m
	 * @throws Exception
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@PathVariable String name, @RequestBody String data, Model m) throws Exception {
		IMetaUpdate update = mr.getMetaUpdate(name);
		Map<String, List<?>> datas = update.update(name, data);
		m.addAllAttributes(datas);
	}
}
